<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\func\Balance as FuncBalance;
use app\common\func\Lib;
use app\common\model\Balance as ModelBalance;
use app\common\model\BalanceTotal;
use think\Db;

class Balance extends Api
{

    protected $noNeedLogin = "*";
    protected $noNeedRight = '*';

    static $moneyType = ["base", "hide"];
    static $searchLimit = 180;

    //现有总余额
    public function getTotal()
    {
        // $total = \app\common\func\Balance::getTotal(2, $this->auth->id);

        $total = ModelBalance::where(["main_type" => 2, "main_id" => $this->auth->id])->value('money');
        $xxuy = \app\common\func\Balance::balance($this->auth->id);
        if ($xxuy) {
            $total = $xxuy;
        }
        $this->success("用户总余额", empty($total) ? '0.00' : $total);
    }

    //用户各交易类型总余额
    public function getTypeTotal()
    {
        $total = \app\common\func\Balance::getTypeTotal(2, $this->auth->id);

        $this->success("用户交易类型总余额", $total);
    }

    //用户各交易类型总余额
    public function getTypePart()
    {
        $balanceType = $this->request->post("balance_type_id");

        $total = \app\common\func\Balance::getTypePart(2, $this->auth->id, $balanceType);

        $this->success("基本类型总余额", $total);
    }

    //余额详情
    public function getMoneyDetails()
    {
        $balanceType = $this->request->post("balance_type_id");
        $moneyType = $this->request->post("money_type");
        $sn = $this->request->post("sn");
        $pageSize = $this->request->post("page_size");
        $startTime = $this->request->post("start_time");
        $endTime = $this->request->post("end_time");

        if ((int)$balanceType > 0) {
            $tranType = Lib::getBalanceTypeById($balanceType);
            if (!$tranType) {
                $this->error("请确认交易类型");
            }
        }

        if ($moneyType != "" && !in_array($moneyType, self::$moneyType)) {
            $this->error("请确认余额类型");
        }

        if ($startTime && $endTime) {
            $start = strtotime($startTime);
            $end = strtotime($endTime);

            if ($start <= $end) {
                $yesterday = strtotime("-1 day");
                $min = strtotime("-" . self::$searchLimit . " day", $yesterday);
                if (date("Y-m-d", $end) > date("Y-m-d", $yesterday)) {
                    $this->error("只能查询昨天以及之前的数据");
                }
                if (date("Y-m-d", $start) < date("Y-m-d", $min)) {
                    $this->error("最大只支持查询" . self::$searchLimit . "天数据");
                }
                $startTime = $start;
                $endTime = strtotime(date("Y-m-d", $end) . " 23:59:59");
            } else {
                $this->error("请确认查询时间");
            }
        } else {
            $yesterday = date("Y-m-d", strtotime("-1 day"));
            $startTime = strtotime($yesterday);
            $endTime = strtotime($yesterday . " 23:59:59");
        }

        $list = \app\common\func\Balance::getMoneyDetails(2, $this->auth->id, $balanceType, $moneyType, $pageSize, $startTime, $endTime, $sn);

        $this->success("余额列表", $list);
    }

    //账户明细
    public function getMoneyLogs()
    {
        $productId = $this->request->post("product_id");
        $balanceType = $this->request->post("balance_type_id");
        $moneyType = $this->request->post("money_type");
        $sn = $this->request->post("sn");
        $pageSize = $this->request->post("page_size");
        $startTime = $this->request->post("start_time");
        $endTime = $this->request->post("end_time");

        $ex = explode("_", $productId);
        $con = count($ex);
        if ((int)$balanceType > 0) {
            if ($con == 1) {
                $tranType = Lib::getBalanceTypeById($balanceType);
                if (!$tranType) {
                    $this->error("请确认交易类型");
                }
            }
        }

        if ($moneyType != "" && !in_array($moneyType, self::$moneyType)) {
            $this->error("请确认余额类型");
        }

        if ($con > 1) {
            $list["data"] =  \app\common\func\Balance::bonus_money_logs($balanceType);
        } else {
            if ($startTime && $endTime) {
                $start = strtotime($startTime);
                $end = strtotime($endTime);

                if ($start <= $end) {
                    $yesterday = strtotime("-1 day");
                    $min = strtotime("-" . self::$searchLimit . " day", $yesterday);
                    if (date("Y-m-d", $end) > date("Y-m-d", $yesterday)) {
                        $this->error("只能查询昨天以及之前的数据");
                    }
                    if (date("Y-m-d", $start) < date("Y-m-d", $min)) {
                        $this->error("最大只支持查询" . self::$searchLimit . "天数据");
                    }
                    $startTime = $start;
                    $endTime = strtotime(date("Y-m-d", $end) . " 23:59:59");
                } else {
                    $this->error("请确认查询时间");
                }
            } else {
                $yesterday = date("Y-m-d", strtotime("-1 day"));
                $startTime = strtotime($yesterday);
                $endTime = strtotime($yesterday . " 23:59:59");
            }
            // $list = \app\common\func\Balance::getMoneyLogs(2,45, $pageSize, $balanceType, $moneyType, $sn, $startTime, $endTime, $productId);
            $list = \app\common\func\Balance::getMoneyLogs(2, $this->auth->id, $pageSize, $balanceType, $moneyType, $sn, $startTime, $endTime, $productId);
            $array = $list->toArray();
            if (!$array["total"]) {
                $array["total"] = 1;
                $array["data"] =  \app\common\func\Balance::bonus_monay_details($this->auth->id, $startTime, $endTime);
                $list = $array;
            }
        }

        $this->success("账户明细", $list);
    }

    /**
     * 获取账户当日结算统计
     */
    public function getAccountDayTotal()
    {
        
        $maxDate = date('Y-m-d', strtotime('-1 day'));
        $date = $this->request->post("date", $maxDate);
        $data = [
            'after' => '0.00',
            'income' => '0.00',
            'income_bit' => 0,
            'expend' => '0.00',
            'expend_bit' => 0
        ];

        $uid = $this->auth->id;
        $typs = 1;
        $dayTyps = $this->request->post("dayTyps");
        if ($dayTyps) $typs = $dayTyps;
        if ($typs == 1) {
            $req = \app\api\controller\balance\Details::instance()->days_account_total($uid, $date);
            if ($req) {
                $data = $req;
            }
        }
        if ($typs == 2) {
            $ex = explode("_", $date);
            if (count($ex) == $dayTyps) {
                $req = \app\api\controller\balance\Details::instance()->range_account_totals($uid, $date);
                if ($req) {
                    $data = $req;
                }
            }
        }

        // $total = BalanceTotal::where(['main_type' => 2, 'main_id' => $this->auth->id, 'date' => $date])->find();
        // if (!empty($total)) {
        //     $data = $total;
        // }
        // if ($data["after"] * 100 == 0) {
        //     $prev_total = BalanceTotal::where(['main_type' => 2, 'main_id' => $this->auth->id])->where('date', '<', $date)->order("date desc")->find();
        //     if (!empty($prev_total)) {
        //         $data["after"] = $prev_total["after"];
        //     }
        // }
        // // 分红
        // $w = "agid = " . $this->auth->id . " and from_unixtime(create_time, '%Y-%m-%d') = '{$date}'";
        // $bonus_money = Db::name("bonus_details")->where($w)->sum("earns");
        // if ((int)$bonus_money > 0) {
        //     $bonus_con = Db::name("bonus_details")->where($w)->count();
        //     $data["after"] += $bonus_money;
        //     $data["income"] += $bonus_money;
        //     $data["income_bit"] += $bonus_con;
        // }
        $this->success("账户明细", $data);
    }

    /**
     * 获取总余额
     */
    public function getBalance()
    {
        $balance = ModelBalance::where(["main_type" => 2, "main_id" => $this->auth->id])->value('money');
        $this->success("余额", empty($balance) ? '0.00' : $balance);
    }
}
